package com.ysd.lis.controller.lab;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.json.XML;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.SysInterfaceMethExtParam;
import com.ysd.lis.entity.SysInterfaceMethod;
import com.ysd.lis.entity.lab.LabUploaditem;
import com.ysd.lis.entity.lab.LabUploaditemVsItem;
import com.ysd.lis.request.DeleteParam;
import com.ysd.lis.service.lab.LabUploaditemService;
import com.ysd.lis.service.lab.LabUploaditemVsItemService;
import com.ysd.lis.util.CA.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 互认项目原因设置表 前端控制器 
 * </p>
 *
 * @author Bai
 * @since 2024-02-02
 */
@RestController
@RequestMapping("/lab/uploadItem")
public class LabUploadItemVsItemController extends BaseController {
    @Autowired
    LabUploaditemVsItemService labUploaditemVsItemService;
    @Autowired
    LabUploaditemService labUploaditemService;

    @ApiOperation("根据申请项目获取对应的报告项目")
    @GetMapping("/findUploadItemVsItemByCode")
    public Result findUploadItemVsItemByCode(String code,String type,String interfaceId,String rptGroup) {
        LambdaQueryWrapper<LabUploaditemVsItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LabUploaditemVsItem::getItemNo,code)
                .eq(LabUploaditemVsItem::getDelFlag,0)
        .eq(LabUploaditemVsItem::getType,type)
        .eq(LabUploaditemVsItem::getInterfaceId,interfaceId);
        if(StrUtil.isNotEmpty(rptGroup)){
            lambdaQueryWrapper.eq(LabUploaditemVsItem::getRptGroup,rptGroup);
        }
        List<LabUploaditemVsItem> list = labUploaditemVsItemService.list(lambdaQueryWrapper);
        return Result.succ(1,"查询成功",list);
    }


    @ApiOperation("添加对照关系")
    @PostMapping("/addUploadItemVsItem")
    public Result addFeeItemVsItemDict(@RequestBody LabUploaditemVsItem labUploaditemVsItem) {
        boolean save = labUploaditemVsItemService.save(labUploaditemVsItem);
        if(save){
            return Result.succ(1,"添加成功","");
        }
        return Result.fail("添加失败");
    }

    @ApiOperation("删除对照关系")
    @PostMapping("/deleteUploadItemVsItem")
    public Result deleteUploadItemVsItem(@RequestBody DeleteParam deleteParam) {
        boolean b = labUploaditemVsItemService.removeById(deleteParam.getId());
        if(b){
            return Result.succ(1,"删除成功","");
        }
        return Result.fail("删除失败");
    }


    //新增互认接口项目对照
    @ApiOperation("获取互认项目")
    @GetMapping("/findUploadItem")
    public Result findUploadItem(String type,String code,String interfaceId,String searchValue,String rptGroup) {
        //查询出已经存在的对照关系
        LambdaQueryWrapper<LabUploaditemVsItem> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(LabUploaditemVsItem::getItemNo,code).eq(LabUploaditemVsItem::getType,type).eq(LabUploaditemVsItem::getDelFlag,0);
        if(StrUtil.isNotEmpty(rptGroup)){
            lambdaQueryWrapper1.eq(LabUploaditemVsItem::getRptGroup,rptGroup);
        }
        List<LabUploaditemVsItem> list1 = labUploaditemVsItemService.list(lambdaQueryWrapper1);
        List<String> collect = list1.stream().map(LabUploaditemVsItem::getUploadItemNo).collect(Collectors.toList());

        LambdaQueryWrapper<LabUploaditem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LabUploaditem::getType,type)
                .eq(LabUploaditem::getDelFlag,0)
                .eq(LabUploaditem::getInterfaceId,interfaceId)
        .notIn(CollectionUtils.isNotEmpty(collect),LabUploaditem::getItemNo,collect);
        if(StrUtil.isNotEmpty(searchValue)){
            lambdaQueryWrapper.and(p->p.like(LabUploaditem::getItemNa,searchValue).or().like(LabUploaditem::getItemNo,searchValue));
        }
        List<LabUploaditem> list = labUploaditemService.list(lambdaQueryWrapper);
        return Result.succ(1,"查询成功",list);
    }


    @ApiOperation("添加互认项目")
    @PostMapping("/addUploadItem")
    public Result addUploadItem(@RequestBody LabUploaditem labUploaditem) {
        boolean save = labUploaditemService.save(labUploaditem);
        if(save){
            return Result.succ(1,"添加成功","");
        }
        return Result.fail("添加失败");
    }

    @ApiOperation("修改互认项目")
    @PostMapping("/editUploadItem")
    public Result editUploadItem(@RequestBody LabUploaditem labUploaditem) {
        boolean save = labUploaditemService.updateById(labUploaditem);
        if(save){
            return Result.succ(1,"添加成功","");
        }
        return Result.fail("添加失败");
    }

    @ApiOperation("删除互认项目")
    @PostMapping("/deleteUploadItem")
    public Result deleteUploadItem(@RequestBody DeleteParam deleteParam) {
        boolean b = labUploaditemService.removeById(deleteParam.getId());
        if(b){
            return Result.succ(1,"删除成功","");
        }
        return Result.fail("删除失败");
    }

    @ApiOperation("上传接口JSON模板")
    @PostMapping("/uploadUploadItem")
    public Result uploadJsonInterfaceMethodParam(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {
        InputStream inputStream = file.getInputStream();
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if(StrUtil.isNotEmpty(line)&&line.indexOf("|")>-1){
                    LabUploaditem labUploaditem = new LabUploaditem();
                    System.out.println(line.split("\\|")[0]);
                    labUploaditem.setItemNo(line.split("\\|")[0]);
                    labUploaditem.setItemNa(line.split("\\|")[1]);
                    labUploaditem.setType("2");
                    labUploaditemService.save(labUploaditem);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail(400, "上传的格式有误", "");
        }


        return Result.succ(1, "导入成功", "");
    }
}